import {
	getMvApi,
	getMvDataApi,
	getRcmdMvApi,
	getArtistApi,
	getArtistFansApi,
	getCommentApi,
	postCommentApi,
	getFollowApi
} from "@/api/api.js"
import {
	myCache
} from "@/utils.js"
export default {
	data() {
		return {
			id: 0,
			mv_url: '',
			poster: '',
			artists: {
				name: '',
				avatar: ''
			},
			mv_data: {},
			rcmd_mv: [],
			artist_data: {},
			fansCnt: 0,
			is_follow: false,
			//弹幕
			pageNo: 1,
			showComments: [], //用来展示的弹幕
			comment: '', //发送弹幕

			videoContext: null
		}
	},
	onReady: function(res) {
		// this.videoContext = document.getElementsByTagName("video");
		this.videoContext = uni.createVideoContext('myVideo');
	},
	onLoad(options) {
		this.id = options.id;
		this.init();
	},
	methods: {
		async init() {
			let data = {
				id: this.id
			};
			let res = await getMvApi(data);
			this.mv_url = res.data.url;
			this.getMvData();
			this.getRcmdMv();
			this.getComment();
		},
		//获取mv数据
		async getMvData() {
			let data = {
				mvid: this.id
			};
			let res = await getMvDataApi(data);
			this.mv_data = res.data;
			this.poster = this.mv_data.cover;
			this.artists.name = this.mv_data.artists[0].name;
			this.artists.avatar = this.mv_data.artists[0].img1v1Url;
			// console.log(this.mv_data);
			this.getArtist();
		},
		//获取推荐mv
		async getRcmdMv() {
			let res = await getRcmdMvApi();
			this.rcmd_mv = res.result;
		},
		//获取歌手信息
		async getArtist() {
			let data = {
				id: this.mv_data.artistId
			};
			let res = await getArtistApi(data);
			this.artist_data = res.data;
			this.getArtistFans();
		},
		//获取歌手粉丝数量
		async getArtistFans() {
			let data = {
				id: this.artist_data.artist.id
			};
			let res = await getArtistFansApi(data);
			this.fansCnt = res.data.fansCnt;
			this.is_follow = res.data.isFollow;
		},
		//获取弹幕评论
		async getComment() {
			let data = {
				id: this.id,
				type: 1,
				sortType: 3,
				pageNo: this.pageNo,
			};
			let res = await getCommentApi(data);
			var comments = res.data.comments;

			for (let i = 0; i < 5; i++) {
				var cursor = comments[comments.length - 1].time;
				let data = {
					id: this.id,
					type: 1,
					sortType: 3,
					pageNo: this.pageNo,
					cursor: cursor || ''
				};
				let result = await getCommentApi(data);
				comments = [...comments, ...result.data.comments];
				this.pageNo++;
			}
			await this.formatComment(comments);
		},
		//弹幕处理
		formatComment(comments) {
			for (let i = 0; i < comments.length; i++) {
				let comment = {
					text: comments[i].content,
					color: this.getRandomColor(),
					time: (i * 2) + 1,
					timeStamp: comments[i].time
				};
				this.showComments.push(comment);
			}
			this.$forceUpdate();
		},
		inputs(e) {
			this.comment = e.detail.value;
		},
		async postComment() {
			let data = {
				t: 1, //1 发送, 2 回复
				type: 1, // 数字,资源类型
				id: this.id,
				content: this.comment,
				cookie: myCache('cookie')
			};
			let res = await postCommentApi(data);
			if (res.code == 200) {
				this.videoContext.sendDanmu({
					text: this.comment,
					color: this.getRandomColor()
				});
				this.comment = '';
			}
		},
		//关注
		async getFollow(t) {
			let data = {
				t,
				id: this.artist_data.artist.id
			};
			let res = await getFollowApi(data);
			if (res.code == 200) {
				this.$showToast(res.followContent);
				this.is_follow = !this.is_follow;
			}
			console.log(res);
		},
		//获取随机字体颜色
		getRandomColor() {
			const rgb = []
			for (let i = 0; i < 3; ++i) {
				let color = Math.floor(Math.random() * 256).toString(16)
				color = color.length == 1 ? '0' + color : color
				rgb.push(color)
			}
			return '#' + rgb.join('')
		},
		formatCount(count) {
			let cnt = Math.round(count / 10000);
			if (cnt >= 1) return cnt + "万";
			else return count;
		},
		goPlayMv(id) {
			const url = `/pages/playmv/playmv?id=${id}`;
			this.$navigateTo(url);
		}
	}
}